package com.modulemanager.models

import com.libbase.MapCodable
import com.libui.utils.ColorUtils

/**
 * Navigation bar style
 */
data class NavigationBarStyle(
    var type: NavigationBarType,
    var backgroundColor: Int?,
    var tintColor: Int?
) : MapCodable {
    companion object {
        fun create(map: Map<String, Any>): NavigationBarStyle? {
            val navigationBarTypeValue = map["type"] as? String ?: return null
            val navigationBarType =
                NavigationBarType.fromValue(navigationBarTypeValue) ?: return null

            val navigationBarBGColor =
                (map["navigationBarBGColor"] as? String)?.let { ColorUtils.toColor(it) }

            val navigationBarTintColor =
                (map["navigationBarTintColor"] as? String)?.let { ColorUtils.toColor(it) }

            return NavigationBarStyle(
                navigationBarType,
                navigationBarBGColor,
                navigationBarTintColor
            )
        }
    }

    /**
     * 导航栏样式
     */
    enum class NavigationBarType(val value: String) {
        HIDE("hide"),
        OPAQUE("opaque"),
        TRANSPARENT("transparent");

        companion object {
            fun fromValue(value: String): NavigationBarType? =
                values().firstOrNull { it.value == value }
        }
    }

    override fun toMap(): Map<String, Any> {
        return mutableMapOf("type" to type.value).apply {
            backgroundColor?.let { put("backgroundColor", ColorUtils.toHexString(it)) }
            tintColor?.let { put("tintColor", ColorUtils.toHexString(it)) }
        }
    }
}